/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package io.iec.edp.caf.i18n.config;

import io.iec.edp.caf.commons.exception.api.GspExceptionMessageService;
import io.iec.edp.caf.commons.exception.core.IExceptionMessageService;
import io.iec.edp.caf.commons.exception.handler.ExceptionMessageService;
import io.iec.edp.caf.i18n.api.*;
import io.iec.edp.caf.i18n.exception.DefaultGspExceptionMessageService;
import io.iec.edp.caf.i18n.framework.api.language.ILanguageService;
import io.iec.edp.caf.i18n.framework.api.resource.IResourceLocalizer;
import io.iec.edp.caf.i18n.framework.localclient.i18nresource.IResourceLocalizerImpl;
import io.iec.edp.caf.i18n.framework.localclient.language.LanguageServiceImpl;
import io.iec.edp.caf.i18n.manager.*;
import io.iec.edp.caf.i18n.utils.CAFLanguageTranslator;
import io.iec.edp.caf.security.core.spring.language.LanguageTranslator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 注册REST服务和Bean
 *
 * @author Vincent Lin
 */
@Configuration(proxyBeanMethods = false)
@EntityScan({"io.iec.edp.caf.i18n.*", "io.iec.edp.caf.commons.exception.*"})
public class I18nConfig {

    @Bean
    public ResourceLocalizer returnResourceLocalizer() {
        return new ResourceLocalizerImpl();
    }

    @Bean
    public GspExceptionMessageService returnDefaultGspExceptionMessageService(ResourceLocalizer manager) {
        return new DefaultGspExceptionMessageService(manager);
    }

    @Bean
    public IResourceLocalizer returnOldResourceLocalizer(ResourceLocalizer resourceLocalizer) {
        return new IResourceLocalizerImpl(resourceLocalizer);
    }

    @Bean
    public IExceptionMessageService returnIExceptionMessageService(ResourceLocalizer resourceLocalizer) {
        return new ExceptionMessageService(resourceLocalizer);
    }

    @Bean
    @ConditionalOnMissingBean(ILanguageService.class)
    public ILanguageService sysInitLanguageService() {
        return new LanguageServiceImpl();
    }

    @Bean
    @ConditionalOnMissingBean(LanguageSuffixProvider.class)
    public LanguageSuffixProvider sysInitLanguageSuffixProvider() {
        return new LanguageSuffixProviderImpl();
    }

    @ConditionalOnMissingBean({CurrentLanguageProvider.class})
    @Bean
    public CurrentLanguageProvider getCurrentLanguageProvider() {
        return new CurrentLanguageProviderImpl();
    }

    @Bean
    public LanguageTranslator defaultLanguageTranslator(LanguageService languageService) {
        return new CAFLanguageTranslator(languageService);
    }
}
